package com.unicom.priceboard.data;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * @author Ho Nguyen
 *
 */
public class BaseXMLParser extends DefaultHandler {
	protected static String today = "";

	public BaseXMLParser() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		today = df.format(new Date(System.currentTimeMillis()));
	}
	
	public void parseDocument(String xmlFile){
		
		try {
			InputStream is = new FileInputStream(new File(xmlFile));
			parseDocument(is);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	
	public void parseDocument(InputStream is) throws IOException {
		
		//get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {		
			//get a new instance of parser
			SAXParser sp = spf.newSAXParser();
			
			//parse the file and also register this class for call backs
			sp.parse(new BufferedInputStream(is), this);
			
		}catch(SAXException se) {
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			pce.printStackTrace();
		}
	}

}
